package com.cvnert.testplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cvnert.testplatform.domain.*;
import com.cvnert.testplatform.domain.vo.DefectListVo;
import com.cvnert.testplatform.domain.vo.DefectVo;
import com.cvnert.testplatform.mapper.*;
import com.cvnert.testplatform.service.DefectService;
import com.cvnert.testplatform.service.DefectVersionService;
import com.cvnert.testplatform.service.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【defect】的数据库操作Service实现
* @createDate 2024-03-24 14:47:45
*/
@Service
public class DefectServiceImpl extends ServiceImpl<DefectMapper, Defect>
    implements DefectService{

    @Resource
    private DefectMapper defectMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DefectVersionService defectVersionService;

    @Resource
    private TaskMapper taskSMapper;

    @Resource
    private VersionMapper versionMapper;

    @Resource
    private DefectVersionMapper defectVersionMapper;

    @Resource
    private DefectTaskMapper defectTaskMapper;

    @Resource
    private TaskMapper taskMapper;



    /**
     * 新增缺陷
     * @param defectVo
     * @return
     */
    @Override
    public Object addDefect(DefectVo defectVo) {
        HashMap<String, Object> res = new HashMap<>();
        // 查看任务是否存在
        Version version = versionMapper.selectById(defectVo.getVid());
        if (version == null) {
            res.put("state", false);
            res.put("msg", "任务不存在");
            return res;
        }
        // 新增缺陷
        Defect defect = new Defect();
        defect.setDefectname(defectVo.getDefectName());
        defect.setDefectlevel(defectVo.getDefectlevel());
        defect.setDefectmain(defectVo.getDefectMain());
        defect.setDefectassign(defectVo.getDefectAssign());
        defect.setDefectperson(defectVo.getDefectPerson());
        defect.setCreatetime(LocalDateTime.now());
        defect.setDefectstatus(defectVo.getDefectStatus());

        DefectVersion defectVersion = new DefectVersion();
        defectVersion.setVid(defectVo.getVid());
        defectVersion.setDid(defect.getId());



        defectVersionService.save(defectVersion);
        defectMapper.insert(defect);

        DefectTask defectTask = new DefectTask();
        defectTask.setDid(defect.getId());
        defectTask.setTid(defectVo.getTid());
        defectTaskMapper.insert(defectTask);

        DefectVersion defectVersion1 = new DefectVersion();
        defectVersion1.setVid(defectVo.getVid());
        defectVersion1.setDid(defect.getId());
        defectVersionMapper.insert(defectVersion1);

        res.put("state", true);
        res.put("msg", "新增成功！");
        return res;
    }

    /**
     * 删除缺陷
     * @param did
     * @return
     */
    @Override
    public Object deleteDefect(Integer did) {
        HashMap<String, Object> res = new HashMap<>();
        Defect defect = defectMapper.selectById(did);
        if (defect == null) {
            res.put("state", false);
            res.put("msg", "缺陷不存在");
            return res;
        }
        int i = defectMapper.deleteById(did);
        if (i > 0) {
            res.put("state", true);
            res.put("msg", "删除成功！");
            return res;
        }
        return res;
    }

    /**
     * 修改缺陷
     * @param defectVo
     * @return
     */
    @Override
    public Object updateDefect(DefectVo defectVo) {
        HashMap<String, Object> res = new HashMap<>();
        Defect defect = defectMapper.selectById(defectVo.getDid());
        if (defect == null) {
            res.put("state", false);
            res.put("msg", "缺陷不存在");
            return res;
        }
        defect.setDefectname(defectVo.getDefectName());
        defect.setDefectlevel(defectVo.getDefectlevel());
        defect.setDefectmain(defectVo.getDefectMain());
        defect.setDefectassign(defectVo.getDefectAssign());
        defect.setDefectperson(defectVo.getDefectPerson());
        defect.setUpdatetime(LocalDateTime.now());
        defectMapper.updateById(defect);
        res.put("state", true);
        res.put("msg", "修改成功！");
        return res;
    }

    /**
     * 获取缺陷信息
     * @param defectListVo
     * @return
     */
    @Override
    public Object getDefectList(DefectListVo defectListVo) {
        HashMap<String, Object> res = new HashMap<>();
        Task task = taskMapper.selectById(defectListVo.getTid());
        if (task == null) {
            res.put("state", false);
            res.put("msg", "任务不存在！");
            return res;
        }
        // 构建查询条件
        QueryWrapper<DefectTask> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("tid", defectListVo.getTid());
        // 通过tid查询did
        List<DefectTask> defectTaskList = defectTaskMapper.selectList(wrapper1);
        defectTaskList.remove(null);
        List<Integer> didList = new ArrayList<>();
        for (DefectTask defectTask : defectTaskList) {
                didList.add(defectTask.getDid());
        }
        didList.remove(null);
        if (didList.size() == 0) {
            res.put("state", false);
            res.put("msg", "数据为空");
            return res;
        }
        List<Defect> defects = defectMapper.selectBatchIds(didList);
        Page<Defect> page = new Page<>();


        page.setTotal(defects.size());
        page.setSize(defectListVo.getPageSize());
        page.setCurrent(defectListVo.getPageNum());
        page.setRecords(defects);
        res.put("data", page);
        res.put("msg", "查询成功！");
        res.put("state", true);

        return res;
    }

    @Override
    public Object getDefectDetail(Integer did) {
        HashMap<String,Object> res = new HashMap<>();
        Defect defect = defectMapper.selectById(did);
        if (defect == null) {
            res.put("state", false);
            res.put("msg", "缺陷不存在");
            return res;
        }else{
            res.put("state", true);
            res.put("msg", "查询成功");
            res.put("data", defect);
        }
        return res;
    }

    @Override
    public Object getDefectCount(DefectListVo defectListVo) {
        HashMap<String, Object> res = new HashMap<>();
        // 获取任务下的所有缺陷
        DefectTask defectTask = defectTaskMapper.selectById(defectListVo.getTid());
        if (defectTask == null) {
            res.put("state", false);
            res.put("msg", "任务不存在");
            return res;
        }
        List<Integer> didList = Collections.singletonList(defectTask.getDid());
        List<Defect> defects = defectMapper.selectBatchIds(didList);
        // 模糊查询
        if (defectListVo.getDefectName() != null){
            List<Defect> list = new ArrayList<>();
            for (Defect defect : defects) {
                if (defect.getDefectname().contains(defectListVo.getDefectName())) {
                    list.add(defect);
                }else {
                    res.put("state", false);
                    res.put("msg", "数据为空");
                    return res;
                }
            }
            defects = list;
        }
        if (defectListVo.getDefectlevel() != null) {
            List<Defect> list = new ArrayList<>();
            for (Defect defect : defects) {
                if (defect.getDefectlevel().contains(defectListVo.getDefectlevel())) {
                    list.add(defect);
                }else {
                    res.put("state", false);
                    res.put("msg", "数据为空");
                    return res;
                }
            }
            defects = list;
        }
        if (defectListVo.getDefectStatus() != null) {
            List<Defect> list = new ArrayList<>();
            for (Defect defect : defects) {
                if (defect.getDefectstatus().contains(defectListVo.getDefectStatus())) {
                    list.add(defect);
                }else {
                    res.put("state", false);
                    res.put("msg", "数据为空");
                    return res;
                }
            }
            defects = list;
        }
        if (defectListVo.getDefectPerson() != null) {
            List<Defect> list = new ArrayList<>();
            for (Defect defect : defects) {
                if (defect.getDefectperson().contains(defectListVo.getDefectPerson())) {
                    list.add(defect);
                }else{
                    res.put("state", false);
                    res.put("msg", "数据为空");
                    return res;
                }
            }
            defects = list;
        }
        if (defectListVo.getDefectAssign() != null) {
            List<Defect> list = new ArrayList<>();
            for (Defect defect : defects) {
                if (defect.getDefectassign().contains(defectListVo.getDefectAssign())) {
                    list.add(defect);
                }else {
                    res.put("state", false);
                    res.put("msg", "数据为空");
                    return res;
                }
            }
            defects = list;
        }

        //将分页后的结果放入响应对象
        res.put("data", defects.size());
        res.put("msg", "查询成功！");
        res.put("state", true);
        return res;
    }

}




